package niumao.minecraft.uhc.listener;

import niumao.minecraft.uhc.Main;
import niumao.minecraft.uhc.Util;
import org.bukkit.ChatColor;
import org.bukkit.enchantments.Enchantment;
import org.bukkit.entity.*;
import org.bukkit.event.EventHandler;
import org.bukkit.event.Listener;
import org.bukkit.event.entity.EntityDamageByEntityEvent;
import org.bukkit.event.entity.EntityShootBowEvent;
import org.bukkit.inventory.ItemStack;
import org.bukkit.inventory.meta.ItemMeta;
import org.bukkit.scheduler.BukkitRunnable;
import org.bukkit.util.Vector;

import java.util.ArrayList;
import java.util.List;
import java.util.Random;

public class apprentice_artemis implements Listener {

    private static final Main plugin = Main.getInstance();

    @EventHandler
    public void on(EntityDamageByEntityEvent event) {
        if (!(event.getDamager() instanceof Player)) {
            if (event.getDamager().getType().equals(EntityType.ARROW)) {
                Arrow arrow = (Arrow) event.getDamager();
                if (!(arrow.getShooter() instanceof Player)) return;
                Player player = (Player) arrow.getShooter();
                ItemStack item = Util.checkInventory(player, Util.getStr("ApprenticeBow"));
                //如果没有Apprentice Bow则返回
                if (item == null) return;

                ItemMeta itemMeta = item.getItemMeta();
                assert itemMeta != null;
                if (itemMeta.getCustomModelData() >= 200) return;
                int sumDamage = itemMeta.getCustomModelData();

                double finalDamage = event.getDamage() + sumDamage;

                //Apprentice Bow的Lore
                List<String> Lore = Util.getStr("ApprenticeBow", true);
                //如果弓箭射到创造玩家身上会造成满额伤害从而数据错误
                if (finalDamage < 200) {
                    if (Main.getLang().equalsIgnoreCase("en_us")) Lore.set(5, "§7Damage dealt: §a" + (int) finalDamage);
                    else Lore.set(5, "§7积累伤害: §a" + (int) finalDamage);
                } else if (finalDamage >= 200 && finalDamage <= 1e5) {
                    if (Main.getLang().equalsIgnoreCase("en_us")) Lore.set(5, "§7Damage dealt: §aMAX");
                    else Lore.set(5, "§7积累伤害: §c已达最大");
                } else return;    //伤害满额返回

                //弓箭伤害判断
                new BukkitRunnable() {
                    @Override
                    public void run() {
                        if (finalDamage >= 200) {
                            upgrade(player, 4, item);
                        } else if (finalDamage >= 100) {
                            upgrade(player, 3, item);
                        } else if (finalDamage >= 50) {
                            upgrade(player, 2, item);
                        } else if (finalDamage >= 25) {
                            upgrade(player, 1, item);
                        }
                    }
                }.runTask(plugin);

                itemMeta.setLore(Lore);
                itemMeta.setCustomModelData((int) finalDamage);
                item.setItemMeta(itemMeta);
            }
        }
    }

    @EventHandler
    public void on2(EntityShootBowEvent e) {
        if (!(e.getEntity() instanceof Player)) return;

        ArrayList<Integer> chanceList = new ArrayList<>();
        chanceList.add(1);
        chanceList.add(2);
        chanceList.add(3);
        chanceList.add(4);
        Random random = new Random();
        int choice = random.nextInt(chanceList.size());
        int finalChoice = chanceList.get(choice);
        LivingEntity player = e.getEntity();
        ItemMeta itemMeta = ((HumanEntity) player).getInventory().getItemInMainHand().getItemMeta();
        if (itemMeta == null) return;

        if (finalChoice == 4 && e.getEntity() instanceof Player
                && e.getProjectile() instanceof Arrow
                && itemMeta.getDisplayName().equals(Util.getStr("Artemis"))) {

            double minAngle = 6.283185307179586D;
            Entity minEntity = null;
            for (Entity entity : player.getNearbyEntities(64.0D, 64.0D, 64.0D)) {
                if (player.hasLineOfSight(entity)
                        && entity instanceof LivingEntity && !entity.isDead()) {
                    Vector toTarget = entity.getLocation().toVector().clone().subtract(player.getLocation().toVector());
                    double angle = e.getProjectile().getVelocity().angle(toTarget);
                    if (angle < minAngle) {
                        minAngle = angle;
                        minEntity = entity;
                    }
                }
            }
            if (minEntity != null)
                new HomingTask((Arrow) e.getProjectile(), (LivingEntity) minEntity, plugin);
        }

    }

    //app弓升级
    private void upgrade(Player player, int level, ItemStack item) {
        //更改物品属性
        ItemMeta itemMeta = item.getItemMeta();
        if (itemMeta.getDisplayName().equalsIgnoreCase(Util.getStr("ApprenticeBow"))
                && itemMeta.getEnchantLevel(Enchantment.ARROW_DAMAGE) == level - 1) {
            String[] str = {"一", "二", "三", "四"};
            itemMeta.addEnchant(Enchantment.ARROW_DAMAGE, level, true);
            item.setItemMeta(itemMeta);
            player.sendMessage("你的武器" + itemMeta.getDisplayName() +
                    ChatColor.GOLD + "升级成功(" + str[level - 1] + "阶)");
        }
    }
}